<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入图片标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item prop="albumType">
        <el-select v-model="queryParams.albumType" placeholder="请选择相册类型" clearable>
          <el-option
            v-for="dict in dict.type.cms_album_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="auditState">
        <el-select v-model="queryParams.auditState" placeholder="请选择审核状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_audit_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="updateTimes">
        <el-date-picker
          v-model="queryParams.updateTimes"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="更新时间"
          end-placeholder="更新时间"
          :default-time="['00:00:00', '23:59:59']"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['cms:album:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['cms:album:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleAudit"
          v-hasPermi="['cms:album:audit']"
        >审核
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleUnaudit"
          v-hasPermi="['cms:album:audit']"
        >反审核
        </el-button>
      </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['cms:album:delete']"
          >删除
          </el-button>
        </el-col>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['cms:album:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="相册类型" align="center" prop="albumType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cms_album_type" :value="scope.row.albumType"/>
        </template>
      </el-table-column>
      <el-table-column label="图片标题" align="center" prop="title" />
      <el-table-column label="文件类型" align="center" prop="fileType" />
      <el-table-column label="图片文件" align="center" prop="imgSrc" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.imgSrc" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="图片描述" align="center" prop="content" />
      <el-table-column label="显示顺序" align="center" prop="sortBy" />
      <el-table-column label="审核状态" align="center" prop="auditState" width="80">
        <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_audit_status" :value="scope.row.auditState"/>
        </template>
      </el-table-column>
      <el-table-column label="审核员" align="center" prop="auditBy" />
      <el-table-column label="审核状态" align="center" prop="auditState" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="260px">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" :disabled="!canEdit(scope.row)"
           @click="handleUpdate(scope.row)" v-hasPermi="['cms:album:edit']"
          >修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-edit"
            @click="handleAudit(scope.row)" v-hasPermi="['cms:album:audit']"
          >审核
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-edit"
            @click="handleUnaudit(scope.row)" v-hasPermi="['cms:album:audit']"
          >反审核
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" :disabled="!canDelete(scope.row)"
            @click="handleDelete(scope.row)"
            v-hasPermi="['cms:album:delete']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.currentPage"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改相册管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="相册类型" prop="albumType">
          <el-select v-model="form.albumType" placeholder="请选择相册类型">
            <el-option
              v-for="dict in dict.type.cms_album_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="图片标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入图片标题" />
        </el-form-item>
        <el-form-item label="图片文件" prop="imgSrc">
          <image-upload v-model="form.imgSrc" :limit="1"/>
        </el-form-item>
        <el-form-item label="图片描述" prop="content">
          <el-input v-model="form.content" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="显示顺序" prop="sortBy">
          <el-input-number v-model="form.sortBy" :min="0" :max="999999999" placeholder="请输入显示顺序" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { canDelete, canEdit } from "@/utils/permission";
import { pageList, queryById, deleteByIds, insert, updateById, pageRelease, pageRecycle, auditByIds, unauditByIds, recoverByIds, removeByIds } from "@/api/cms/album";

export default {
  name: "Album",
  dicts: ['sys_audit_status', 'cms_album_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        currentPage: 1,
        pageSize: 20,
        albumType: null,
        title: null,
        fileType: null,
        imgSrc: null,
        content: null,
        sortBy: null,
        auditState: null,
        editState: null,
        updateTimes: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        albumType: [ { required: true, message: "相册类型不能为空", trigger: "change" } ],
        title: [ { required: true, message: "图片标题不能为空", trigger: "blur" } ],
        fileType: [ { required: true, message: "文件类型不能为空", trigger: "change" } ],
        imgSrc: [ { required: true, message: "图片文件不能为空", trigger: "blur" } ],
        sortBy: [ { required: true, message: "显示顺序不能为空", trigger: "blur" } ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    canDelete,
    canEdit,
    /** 查询相册管理列表 */
    getList() {
      this.loading = true;
      pageList(this.queryParams).then(resultDTO => {
        this.list = resultDTO.data?.list;
        this.total = resultDTO.data?.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        albumType: 'home',
        title: null,
        fileType: null,
        thumbnail: null,
        imgSrc: null,
        content: null,
        sortBy: 100,
        auditState: 0,
        editState: 0,
        updateBy: null,
        updateTime: null,
        createBy: null,
        createTime: null,
        auditBy: null,
        auditTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.currentPage = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selecteds = selection;
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加相册管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      if (!row.id) {
        if(this.selecteds.some((item)=> item.editState > 1)) {
          this.$modal.msgWarning("系统数据不能编辑");
          return;
        }
        if(this.selecteds.some((item)=> item.auditState > 0)) {
          this.$modal.msgWarning("已审核数据不能编辑");
          return;
        }
      }
      this.reset();
      const id = row.id || this.ids
      queryById(id).then(resultDTO => {
        this.form = resultDTO.data;
        this.open = true;
        this.title = "修改相册管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateById(this.form).then(resultDTO => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            insert(this.form).then(resultDTO => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 审核按钮操作 */
    handleAudit(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否审核相册管理编号为"' + ids + '"的数据项？').then(function () {
        return auditByIds(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("审核成功");
      }).catch(() => {});
    },
    /** 反审核按钮操作 */
    handleUnaudit(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否反审核相册管理编号为"' + ids + '"的数据项？').then(function () {
        return unauditByIds(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("反审核成功");
      }).catch(() => {});
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      if (!row.id) {
        if(this.selecteds.some((item)=> item.editState > 0)) {
          this.$modal.msgWarning("系统数据不能删除");
          return;
        }
        if(this.selecteds.some((item)=> item.auditState > 0)) {
          this.$modal.msgWarning("已审核数据不能删除");
          return;
        }
      }
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除相册管理编号为"' + ids + '"的数据项？').then(function () {
        return deleteByIds(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 逻辑删除按钮操作 */
    handleRemove(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认逻辑删除相册管理编号为"' + ids + '"的数据项？').then(function () {
        return removeByIds(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 还原删除按钮操作 */
    handleRecover(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认还原删除相册管理编号为"' + ids + '"的数据项？').then(function () {
        return recoverByIds(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("还原成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('cms/album/export', {
        ...this.queryParams
      }, `album_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
